<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录 - 文件管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        body {
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .login-container {
            background-color: white;
            padding: 2rem 3rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        h1 {
            text-align: center;
            margin-bottom: 1.5rem;
            color: #1a73e8;
        }
        .form-group {
            margin-bottom: 1rem;
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: #5f6368;
        }
        input {
            width: 100%;
            padding: 0.8rem;
            border: 1px solid #dadce0;
            border-radius: 5px;
            font-size: 1rem;
        }
        input:focus {
            outline: none;
            border-color: #1a73e8;
            box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
        }
        button {
            width: 100%;
            padding: 0.8rem;
            background-color: #1a73e8;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        button:hover {
            background-color: #1557b0;
        }
        .error-message {
            color: #d93025;
            text-align: center;
            margin-top: 1rem;
            display: none;
        }
        .register-link {
            text-align: center;
            margin-top: 1rem;
            color: #5f6368;
        }
        .register-link a {
            color: #1a73e8;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>文件管理系统</h1>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit">登录</button>
            <div class="error-message" id="errorMessage"></div>
            <div class="register-link">
                还没有账号? <a href="/register">立即注册</a>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            const errorElement = document.getElementById('errorMessage');
            errorElement.style.display = 'none';

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            try {
                const response = await fetch('/api/user/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password }),
                });

                const data = await response.json();

                if (response.ok) {
                    // 存储token到localStorage
                    localStorage.setItem('token', data.token);
                    // 跳转到文件管理页面
                    window.location.href = '/files';
                } else {
                    errorElement.textContent = data.error || '登录失败，请检查用户名和密码';
                    errorElement.style.display = 'block';
                }
            } catch (error) {
                errorElement.textContent = '网络错误，请稍后重试';
                errorElement.style.display = 'block';
                console.error('Login error:', error);
            }
        });
    </script>
</body>
</html>